package com.all.play.mymd.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.mymd.R;

import java.util.ArrayList;
import java.util.List;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener{

    private List<String> datalist;
    private ListPopupWindow listPopupWindow;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        initData();


    }



    private void initData() {
        datalist = new ArrayList<>();
        for (int i=0;i<50;i++){
            datalist.add("hashiqi  "+i);
        }
    }


    /**
     * PupuWindow
     * https://blog.csdn.net/harvic880925/article/details/49272285
     * @param view
     */
    public void showPopu(View view){
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datalist);
        View contentview = LayoutInflater.from(this).inflate(R.layout.popuwindows, null);
        popupWindow = new PopupWindow(contentview, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setContentView(contentview);

        //设置各个控件的点击响应
        TextView tv1 = (TextView)contentview.findViewById(R.id.pop_computer);
        TextView tv2 = (TextView)contentview.findViewById(R.id.pop_financial);
        TextView tv3 = (TextView)contentview.findViewById(R.id.pop_manage);
        tv1.setOnClickListener(SecondActivity.this);
        tv2.setOnClickListener(SecondActivity.this);
        tv3.setOnClickListener(SecondActivity.this);

        //popuwindow所添加的父布局
        View rootview = LayoutInflater.from(SecondActivity.this).inflate(R.layout.activity_second, null);

        //设置动画
        popupWindow.setAnimationStyle(R.style.contextMenuAnim);
        //设置位置
        popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);

    }

    /**
     * ListPopupWindow
     * @param view
     */
    public void showListPopu(View view){

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datalist);
        listPopupWindow = new ListPopupWindow(this);
        listPopupWindow.setAdapter(adapter);

        //弹出的位置  弹出锚点
        listPopupWindow.setAnchorView(view);
        //设置宽高
        listPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

        //listPopupWindow.setWidth(view.getWidth());
        //listPopupWindow.setHeight(this.getWindowManager().getDefaultDisplay().getHeight()/2);

//        listPopupWindow.show();//假如监听中要dismiss掉,显示方法不能写在点击监听之前

        listPopupWindow.setModal(true);//设置模态框，设置为true响应物理键

        // ListPopupWindow 距锚view的距离
//        listPopupWindow.setHorizontalOffset(50);
//        listPopupWindow.setVerticalOffset(100);


        //设置条目点击事件
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.i("retertere", "onItemClick: "+position);
                Toast.makeText(SecondActivity.this, "位置是: "+position, Toast.LENGTH_SHORT).show();
                listPopupWindow.dismiss();
            }
        });


        listPopupWindow.show();



    }

    /**
     * PopupMenu
     * @param view
     */
    public void showPopuMenu(View view){

        PopupMenu popupMenu=new PopupMenu(this,view);
        popupMenu.getMenuInflater().inflate(R.menu.main,
                popupMenu.getMenu());

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(SecondActivity.this, ""+item.getItemId(), Toast.LENGTH_SHORT).show();

                return true;
            }
        });
        popupMenu.show();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.pop_computer:{
                Toast.makeText(this,"clicked computer",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
            break;
            case R.id.pop_financial:{
                Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
            break;
            case R.id.pop_manage:{
                Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
            break;
        }
    }
}
